<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 400px;
            margin: 100px auto;
            background-color: #ccc;
            line-height: 200px;
            font-size: 20px;
            text-align: center;
        }
    </style>

</head>

<body>

    <div class="box">
        剩余时间：1天 12:03:32
    </div>

    <script>


        function getTime(startTime, endTime) {
            var date = new Date;
            var nowTime = date.getTime();

            var startTime = new Date(startTime).getTime();
            var startTimeDiff = nowTime - startTime;
            if (startTimeDiff <= 0) {
                return box.innerHTML = '秒杀活动未开始';
            }

            // var dateNow = +new Date();
            var endTime = +new Date(endTime);
            var endTimeDiff = endTime - nowTime;
            if (endTimeDiff <= 0) {
                clearInterval(timer)
                return box.innerHTML = '您已错过秒杀活动时间'
            } else {
                return box.innerHTML = count(endTimeDiff)
            }
        }
        function count(times) {
            times = parseInt(times / 1000);
            var d = parseInt(times / 60 / 60 / 24); // 天
            d = zeroFill(d);
            var h = parseInt(times / 60 / 60 % 24); //时
            h = zeroFill(h);
            var m = parseInt(times / 60 % 60); // 分
            m = zeroFill(m);
            var s = parseInt(times % 60); // 当前的秒
            s = zeroFill(s);
            return '秒杀剩余时间： ' + d + '天 ' + h + '：' + m + '：' + s;
        }
        function zeroFill(num) {
            return num < 10 ? '0' + num : num;
        }

        var timer = setInterval(function () {
            getTime('2022-4-21 00:00:00', '2022-4-23 00:00:00');
        }, 100);

        var box = document.querySelector('.box');


    </script>

</body>

</html>